package com.woniu.his.service.impl;

import com.woniu.his.dao.FinanceMapper;
import com.woniu.his.entity.*;
import com.woniu.his.service.IFinanceService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class FinanceServiceImpl implements IFinanceService {
    @Autowired
    private FinanceMapper financeMapper;

    //查询所有的项目清单
    @Override
    public List<BillInfo> findAllBills() {
        List<BillInfo> billInfos = financeMapper.findAllBills();
        System.out.println(billInfos);
        if (billInfos != null) {
            return billInfos;
        }
        return null;
    }


    //根据姓名或者就诊信息编号查询某个人的项目清单
    @Override
    public List<BillInfo> findByMedicalIdPatientName(int medicalId) {
        List<BillInfo> billInfos = financeMapper.findByMedicalIdPatientName(medicalId);
        System.out.println(billInfos);
        if (billInfos != null) {
            return billInfos;
        }
        return null;
    }

//    //将查询出来的缴费记录插入数据库
//    @Override
//    public int addIncomeRecord(String patientName1, int medicalId1, String incomeDetail, BigDecimal incomePrice, int incomeCount, BigDecimal totalPrice, Date incomeTime, int status) {
//        int i = financeMapper.addIncomeRecord(patientName1, medicalId1, incomeDetail, incomePrice, incomeCount, totalPrice, incomeTime, status);
//        if(i>0){
//            return i;
//        }
//        return 0;
//    }

    @Override
    public int updateStatus() {
        int i = financeMapper.updateStatus();
        System.out.println(i);
        if (i > 0) {
            return i;
        }
        return 0;
    }

    @Override
    public List<IncomeRecord> findIncomeRecord() {
        List<IncomeRecord> incomeRecord = financeMapper.findIncomeRecord();
        System.out.println(incomeRecord);
        if (incomeRecord != null) {
            return incomeRecord;
        }
        return null;
    }

    @Override
    public BigDecimal findTotalIncome() {
        BigDecimal totalIncome = financeMapper.findTotalIncome();
        if (totalIncome != null) {
            return totalIncome;
        }
        return null;
    }

    @Override
    @Transactional
    public String updateIncomeInfo(int payId, String patientName, int medicalId, String payProject, double price, int count, double totalPrice) {
        PayInfo payInfo = new PayInfo();
        payInfo.setPayTime(new Date());
        payInfo.setPayId(payId);
        payInfo.setStatus(1);
        financeMapper.updateById(payInfo);
        //并将数据添加进incomeInfo表
        Subject subject = SecurityUtils.getSubject();
        DocterInfo principal = (DocterInfo) subject.getPrincipal();
        BigDecimal bigDecimalPrice = new BigDecimal(price);
        BigDecimal bigDecimalTotalPrice = new BigDecimal(totalPrice);
        BigDecimal bigDecimalCount = new BigDecimal(count);
//        int i = financeMapper.addIncomeRecord(patientName, medicalId, payProject, bigDecimalPrice, count, bigDecimalTotalPrice, new Date(), 1, principal.getDocterId());
        int i = financeMapper.addIncomeRecord(patientName, medicalId, payProject, bigDecimalPrice, count, bigDecimalTotalPrice, new Date(), 1, 1);

        return "操作成功";
    }
}
